package commons.base.js;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import commons.base.serialization.json.JsonExample;
import commons.base.serialization.json.JsonExample.User;

public class ScriptExample {

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		
		String json = readJson("user.json");
		
		List<User> list = JSON.parseArray(json, User.class);
//		new Script().exec(json);
		
		long end = System.currentTimeMillis();
		System.err.println("t1 : " + (end -start));
		System.err.println(list.size());
	}
	
	private static String readJson(String name) throws Exception{
		Path path = Paths.get(JsonExample.class.getResource("/" + name).toURI());
		
		return new String(Files.readAllBytes(path), "UTF-8");
	}
	
	public static void genJson()throws Exception{
		JSONArray arr = new JSONArray();
		for(int i=0; i<5000; i++){
			JSONObject obj = new JSONObject();
			obj.put("nick", "hz0799");
			obj.put("sex", "m");
			obj.put("avatar", "http://assets.taobaocdn.com/app/sns/img/default/avatar-120.png");
			arr.add(obj);
		}
		
		Files.write(Paths.get("/data/user.json"), JSON.toJSONString(arr, true).getBytes("UTF-8"));
//		System.err.println(JSON.toJSONString(arr, true));
	}

}
